package com.whcoding.test.common;

import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import de.odysseus.staxon.json.JsonXMLConfig;
import de.odysseus.staxon.json.JsonXMLConfigBuilder;
import de.odysseus.staxon.json.JsonXMLInputFactory;
import de.odysseus.staxon.json.JsonXMLOutputFactory;
import de.odysseus.staxon.xml.util.PrettyXMLEventWriter;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

/**
 * @program: spring-boot-learning
 * @description: XML 转为JSON
 * @author: whcoding
 * @create: 2022-04-12 17:57
 **/
public class XmlJsonUtil {


	/**
	 * json string convert to xml string
	 */
	public static String json2xml(String json) {
		StringReader input = new StringReader(json);
		StringWriter output = new StringWriter();
		JsonXMLConfig config = new JsonXMLConfigBuilder().multiplePI(false)
				.repairingNamespaces(false).build();
		try {
			XMLEventReader reader = new JsonXMLInputFactory(config)
					.createXMLEventReader(input);
			XMLEventWriter writer = XMLOutputFactory.newInstance()
					.createXMLEventWriter(output);
			writer = new PrettyXMLEventWriter(writer);
			writer.add(reader);
			reader.close();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				output.close();
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (output.toString().length() >= 38) {
			// remove <?xml version="1.0" encoding="UTF-8"?>
			return output.toString().substring(39);
		}
		return output.toString();
	}

	/**
	 * XML转为JSON
	 * @param xml
	 * @return
	 * @throws JSONException
	 */
	public static String xml2jsonString(String xml) throws JSONException{
		JSONObject xmlJSONObj = XML.toJSONObject(xml);
		return xmlJSONObj.toString();
	}

}






